package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.converter.CheckGroupConverter;
import com.itheima.dto.CheckGroupDTO;
import com.itheima.dto.CheckGroupPageQueryDTO;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemAndCheckItemGroupMapper;
import com.itheima.mapper.SetmealAndCheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckItemAndCheckItemGroupMapper checkItemAndCheckItemGroupMapper;
    @Autowired
    private SetmealAndCheckGroupMapper setmealAndCheckGroupMapper;
    /**
     * 新增检查组
     * @param checkGroupDTO
     */
    @Override
    @Transactional
    public void saveWithCheckItem(CheckGroupDTO checkGroupDTO, List<String> checkitemIds) {
        CheckGroup checkGroup = CheckGroupConverter.checkGroupDTOtoCheckGroup(checkGroupDTO);
        checkGroupMapper.insert(checkGroup);
        //获取insert中的主键值id
        int checkGroupId = checkGroup.getId();
        //转换String集合为Integer集合
        List<Integer> checkItemIdsInt = new ArrayList<>();
        if (checkitemIds != null && !checkitemIds.isEmpty()) {
           for (String checkitemId : checkitemIds) {
               Integer checkItemIdInt = Integer.parseInt(checkitemId);
               checkItemIdsInt.add(checkItemIdInt);
           }
         }
        checkItemAndCheckItemGroupMapper.insert(checkGroupId,checkItemIdsInt);

    }

    /**
     * 删除检查组
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(String id) {
         Integer checkGroupId = Integer.parseInt(id);
        //先删除关联表，在删除本表
         //查询关联表相关数据是否存在
         List<Integer> checkId =checkItemAndCheckItemGroupMapper.getCheckGroupRelevancyCheckitemByCheckGroupId(checkGroupId);
         if(!Objects.isNull(checkId)){
             //删除关联表数据
             checkItemAndCheckItemGroupMapper.delectCheckGroupRelevancyCheckitemByCheckGroupId(checkGroupId);
         }
         //删除检查组数据
        checkGroupMapper.deleteById(checkGroupId);
    }

    /**
     * 根据套餐id查询关联的检查项id的套餐id
     * @param setmealId
     * @return
     */
    @Override
    public List<Integer> findCheckItemIdsBysetmealId(String setmealId) {
        Integer setMealId = Integer.parseInt(setmealId);
        //通过setmealId查询关联表中checkGroupId
        List<Integer> checkGroupIds = setmealAndCheckGroupMapper.getCheckGroupIdBysetmealId(setMealId);
//        //判断集合是否存在
//        if (checkGroupIds != null && !checkGroupIds.isEmpty()) {
//            for (Integer checkGroupId : checkGroupIds) {
//                List<Integer> checkItemIds = checkItemAndCheckItemGroupMapper.getCheckGroupRelevancyCheckitemByCheckGroupId(checkGroupId);
//            }
//        }
      return checkGroupIds;
    }

    /**
     * 分页查询
     * @param checkGroupPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CheckGroupPageQueryDTO checkGroupPageQueryDTO) {
        PageHelper.startPage(checkGroupPageQueryDTO.getCurrentPage(), checkGroupPageQueryDTO.getPageSize());
        Page<CheckGroup> checkGroupPage=checkGroupMapper.pageQuery(checkGroupPageQueryDTO);
        PageResult pageResult = new PageResult(checkGroupPage.getTotal(), checkGroupPage.getResult());
        return pageResult;
    }

    /**
     * 编辑
     * @param checkgroupDTO
     * @param checkitemIds
     */
    @Override
    @Transactional
    public void updateWithCheckItem(CheckGroupDTO checkgroupDTO, List<String> checkitemIds) {
        CheckGroup checkGroup = CheckGroupConverter.checkGroupDTOtoCheckGroup(checkgroupDTO);
        checkGroupMapper.update(checkGroup);
        //删除关联表项
        checkItemAndCheckItemGroupMapper.delectCheckGroupRelevancyCheckitemByCheckGroupId(checkgroupDTO.getId());
        //重新添加关联表
        List<Integer> checkItemIdsInt = new ArrayList<>();
        if (checkitemIds != null && !checkitemIds.isEmpty()) {
            for (String checkitemId : checkitemIds) {
                Integer checkItemIdInt = Integer.parseInt(checkitemId);
                checkItemIdsInt.add(checkItemIdInt);
            }
        }
        checkItemAndCheckItemGroupMapper.insert(checkGroup.getId(), checkItemIdsInt);

    }

    /**
     * 查询所有项
     * @return
     */
    @Override
    public List<CheckGroup> findAll() {

        List<CheckGroup> checkGroupList=checkGroupMapper.findAll();
        return checkGroupList;
    }

    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(String id) {
        Integer checkId = Integer.parseInt(id);
        CheckGroup checkGroup =checkGroupMapper.findById(checkId);
        return checkGroup;
    }
}
